package com.yy.satokenapplication;

import com.yy.satokenapplication.entity.SysPermission;
import com.yy.satokenapplication.entity.SysRole;
import com.yy.satokenapplication.entity.SysUser;
import com.yy.satokenapplication.mapper.SysPermissionMapper;
import com.yy.satokenapplication.mapper.SysRoleMapper;
import com.yy.satokenapplication.mapper.SysUserMapper;
import com.yy.satokenapplication.util.PasswordEncryptor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;

/**
 * 普通用户（如张三）：
 * 只能查看图书列表（book:query）
 * 不能新增、修改、删除图书
 *
 * 管理员（如李四）：
 * 可以查看图书列表（book:query）
 * 可以新增图书（book:add）
 * 可以修改图书（book:edit）
 * 可以删除图书（book:delete）
 */
@SpringBootTest
class SatokenApplicationTests {

    @Autowired
    private PasswordEncryptor passwordEncryptor;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysPermissionMapper sysPermissionMapper;
    @Test
    void contextLoads() {
        System.out.println(passwordEncryptor.encrypt("123456"));
    }
    
    @Test
    void test1() {
        System.out.println(sysRoleMapper.findRolesByUserId(2L));
    }
    
    @Test
    void test2() {
        SysUser user = sysUserMapper.findUserWithRolesAndPermissionsById(2L);
        System.out.println(user.getRolesList().stream().map(SysRole::getRoleCode).toList());
        System.out.println(user.getPermissionsList().stream().map(SysPermission::getCode).toList());
    }
    @Test
    void test3() {
        System.out.println(sysPermissionMapper.findPermissionsByUserId(2L));
    }

    public static void printTree(File file, int level) {
        StringBuilder indent = new StringBuilder();
        for (int i = 0; i < level; i++) {
            indent.append("  ");
        }
        if (level > 0) {
            indent.append("└─ ");
        }
        System.out.println(indent + file.getName());

        if (file.isDirectory()) {
            File[] children = file.listFiles();
            if (children != null) {
                for (File child : children) {
                    printTree(child, level + 1);
                }
            }
        }
    }
    
    @Test
    void test4() {
        // 指定要打印结构树的目录
        File root = new File("C:\\Users\\young\\Desktop\\2020文档集\\cursor_project\\satokenApplication\\src");
        printTree(root, 0);
    }
}
